//----------------------------------------------------
// T7Game - Copyright (c) 湖南淘气网络科技有限公司
// Author:
// Date:2017/6/6
// Description:
//----------------------------------------------------

using UnityEngine;
using System.Collections;

public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T s_instance = null;

    public static T instance
    {
        get
        {
            if (null == s_instance)
            {
                s_instance = FindObjectOfType(typeof(T)) as T;  //查询T类型的对象,只能是激活的对象

                if (null == s_instance)
                {
                   Debug.LogError(typeof(T) + " 没有实例化！");
                }

            }
            else if (s_instance.gameObject == null)
            {
                Debug.LogError(typeof(T) + " 没有清理干净！");
            }
            return s_instance;
        }
    }

    void Awake()
    {
        s_instance = this as T;
        OnAwake();
    }

    void Start()
    {
        OnStart();
    }

    void OnDestroy()
    {
        OnOnDestroy();
        s_instance = null;
    }

    protected virtual void OnAwake()
    {
    }

    protected virtual void OnStart()
    {
    }

    protected virtual void OnOnDestroy()
    {

    }
}
